<template>
	<view>
		<!-- 注册界面的背景图 -->
		<view>
			<image class="backgroundp" src="/static/bg.png"></image>
		</view>
		<!-- 注册界面的中间提示文字 -->
		<view class="load">
			<text class="text">欢迎注册!</text>
		</view>
		<!-- 分步骤表单 -->
		<view class="form-content">
			<!-- 步骤指示器 -->
			<view class="steps">
				<view :class="['step', currentStep === 1 ? 'active' : '']">
					<text>1</text>
					<text>账号信息</text>
				</view>
				<view :class="['step', currentStep === 2 ? 'active' : '']">
					<text>2</text>
					<text>个人信息</text>
				</view>
			</view>
			<!-- 步骤 1：账号信息 -->
			<view v-if="currentStep === 1" class="step-content">
				<u-form :model="form" ref="uFormStep1" label-width="50">
					<!-- 账号输入框 -->
					<u-form-item prop="username">
						<u-input prefixIcon="account-fill" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.username" clearable shape="circle" :adjustPosition="false"
							maxlength=20
							placeholder="请输入账号（用于登录）" />
					</u-form-item>
					<!-- 密码输入框 -->
					<u-form-item prop="password">
						<u-input prefixIcon="lock" type="password" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.password" clearable shape="circle" :adjustPosition="false"
							maxlength=16
							placeholder="请输入密码" />
					</u-form-item>
					<!-- 确认密码输入框 -->
					<u-form-item prop="confirmPassword">
						<u-input prefixIcon="lock" type="password" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.confirmPassword" clearable shape="circle" :adjustPosition="false"
							maxlength=16
							placeholder="请确认密码" />
					</u-form-item>
				</u-form>
				<!-- 下一步按钮 -->
				<u-button class="custom-style" shape="circle" size="medium"
					@click="nextStep">下一步</u-button>
			</view>
			<!-- 步骤 2：个人信息 -->
			<view v-if="currentStep === 2" class="step-content">
				<u-form :model="form" ref="uFormStep2" label-width="50">
					<!-- 昵称输入框 -->
					<u-form-item prop="name">
						<u-input prefixIcon="edit-pen" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.name" clearable shape="circle" :adjustPosition="false"
							maxlength=20
							placeholder="请输入昵称" />
					</u-form-item>
					<!-- 性别选择 -->
					<u-form-item prop="gender">
						<u-radio-group v-model="form.gender">
							<u-radio name="male" label="男"></u-radio>
							<u-radio name="female" label="女"></u-radio>
						</u-radio-group>
					</u-form-item>
					<!-- 年龄输入框 -->
					<u-form-item prop="age">
						<u-input prefixIcon="calendar" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.age" clearable shape="circle" :adjustPosition="false"
							type="number"
							placeholder="请输入年龄" />
					</u-form-item>
					<!-- 体重输入框 -->
					<u-form-item prop="weight">
						<u-input prefixIcon="heart" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.weight" clearable shape="circle" :adjustPosition="false"
							type="number"
							placeholder="请输入体重 (kg)" />
					</u-form-item>
					<!-- 身高输入框 -->
					<u-form-item prop="height">
						<u-input prefixIcon="coupon" prefixIconStyle="font-size: 22px;color: #409EFF"
							v-model="form.height" clearable shape="circle" :adjustPosition="false"
							type="number"
							placeholder="请输入身高 (cm)" />
					</u-form-item>
				</u-form>
				<!-- 上一步按钮 -->
				<u-button class="custom-style prev-button" shape="circle" size="medium"
					@click="prevStep">上一步</u-button>
				<!-- 提交按钮 -->
				<u-button class="custom-style submit-button" :disabled="isRegistering" :loading="isRegistering" shape="circle" size="medium"
					@click="registerRequest">提交</u-button>
			</view>
			<!-- 登录文字超链接 -->
			<view class="login-link">
				<text @click="navigateToLogin">已有账号？立即登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import globalSetting from '@/common/json/globalSetting.json'; // 全局配置

	export default {
		data() {
			return {
				currentStep: 1, // 当前步骤
				isRegistering: false, // 注册加载提示
				form: {
					username: '', // 账号
					password: '', // 密码
					confirmPassword: '', // 确认密码
					name: '', // 昵称
					gender: 'male', // 性别，默认为男
					age: '', // 年龄
					weight: '', // 体重
					height: '' // 身高
				},
				// 表单验证规则
				rulesStep1: {
					username: [{
						required: true,
						message: '请输入账号',
						trigger: ["change", "blur"],
					}, {
						pattern: /^[0-9a-zA-Z]*$/g, // 只能包含字母或数字
						transform(value) {
							return String(value);
						},
						message: '只能包含字母或数字',
						trigger: ["change", "blur"],
					}, {
						min: 3,
						max: 20,
						message: "账号长度为3~20位",
						trigger: ["change", "blur"],
					}],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							pattern: /^[0-9a-zA-Z]*$/g, // 只能包含字母或数字
							transform(value) {
								return String(value);
							},
							message: '只能包含字母或数字',
							trigger: ["change", "blur"],
						},
						{
							min: 6,
							max: 16,
							message: "密码长度为6~16位",
							trigger: ["change", "blur"],
						}
					],
					confirmPassword: [{
							required: true,
							message: '请确认密码',
							trigger: 'blur'
						},
						{
							validator: (rule, value, callback) => {
								if (value !== this.form.password) {
									callback(new Error('两次输入的密码不一致'));
								} else {
									callback();
								}
							},
							trigger: ["change", "blur"],
						}
					]
				},
				rulesStep2: {
					name: [{
						required: true,
						message: '请输入昵称',
						trigger: ["change", "blur"],
					}, {
						min: 2,
						max: 20,
						message: "昵称长度为2~20位",
						trigger: ["change", "blur"],
					}],
					gender: [{
						required: true,
						message: '请选择性别',
						trigger: ["change", "blur"],
					}],
					age: [{
						required: true,
						message: '请输入年龄',
						trigger: ["change", "blur"],
					}, {
						pattern: /^\d+$/,
						message: '年龄必须为数字',
						trigger: ["change", "blur"],
					}],
					weight: [{
						required: true,
						message: '请输入体重',
						trigger: ["change", "blur"],
					}, {
						pattern: /^\d+(\.\d+)?$/,
						message: '体重必须为数字',
						trigger: ["change", "blur"],
					}],
					height: [{
						required: true,
						message: '请输入身高',
						trigger: ["change", "blur"],
					}, {
						pattern: /^\d+(\.\d+)?$/,
						message: '身高必须为数字',
						trigger: ["change", "blur"],
					}]
				}
			};
		},

		// 页面加载时设置标题
		onLoad() {
			uni.setNavigationBarTitle({
				title: "注册",
			});
		},

		methods: {
			// 显示提示信息
			showPrompt(text) {
				uni.showModal({
					content: text,
					showCancel: false,
				});
			},

			// 下一步
			nextStep() {
				this.$refs.uFormStep1.validate().then(res => {
					this.currentStep = 2;
					// 动态设置第二步的表单规则
					this.$nextTick(() => {
						if (this.$refs.uFormStep2) {
							this.$refs.uFormStep2.setRules(this.rulesStep2);
						}
					});
				}).catch(error => {
					this.showPrompt("请填写正确的账号信息");
				});
			},

			// 上一步
			prevStep() {
				this.currentStep = 1;
			},

			// 注册请求
			async registerRequest() {
				this.isRegistering = true; // 显示加载提示

				this.$refs.uFormStep2.validate().then(async res => {
					// 发起注册请求
					await uni.request({
						url: `${this.$baseUrl}/user/registerService`, // 注册接口
						method: "POST",
						data: {
							id: this.form.username,//账号
							password: this.form.password,
							name: this.form.name, // 昵称
							gender: this.form.gender,
							age: this.form.age,
							weight: this.form.weight,
							height: this.form.height
						},
						timeout: globalSetting.TIME_OUT,
					}).then((data) => {
						var [err, res] = data;
						if (err) {
							this.showPrompt("网络错误，请稍后重试");
						} else if (res.data.success) {
							this.navigateToLogin(); 
						} else {
							this.showPrompt("注册失败，可能账户已存在");
						}
					});
				}).catch(error => {
					this.showPrompt("请填写完整的个人信息");
				}).finally(() => {
					this.isRegistering = false; // 隐藏加载提示
				});
			},

			// 跳转到登录页面
			navigateToLogin() {
				uni.navigateTo({
					url: "/pages/index/login" 
				});
			}
		},

		// 设置表单验证规则
		onReady() {
			this.$refs.uFormStep1.setRules(this.rulesStep1);
			// this.$refs.uFormStep2.setRules(this.rulesStep2);
		}
	};
</script>

<style scoped lang="scss">
	// 注册界面的背景图
	.backgroundp {
		z-index: -1;
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
	}

	// 页面背景中间的提示文字
	.load {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 100rpx;

		.text {
			font-size: 50rpx;
			font-weight: bold;
			color: #333;
		}
	}

	// 表单容器
	.form-content {
		margin: 40rpx auto;
		padding: 40rpx;
		background-color: #fff;
		border-radius: 24rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
		width: 90%;
		max-width: 600rpx;
	}

	// 步骤指示器
	.steps {
		display: flex;
		justify-content: space-between;
		margin-bottom: 40rpx;

		.step {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 28rpx;
			color: #909399;

			&.active {
				color: #409EFF;
				font-weight: bold;

				text:first-child {
					background-color: #409EFF;
					color: #fff;
					border-radius: 50%;
					padding: 10rpx;
					width: 40rpx;
					height: 40rpx;
					text-align: center;
					line-height: 40rpx;
				}
			}

			text:first-child {
				background-color: #f0f0f0;
				color: #909399;
				border-radius: 50%;
				padding: 10rpx;
				width: 40rpx;
				height: 40rpx;
				text-align: center;
				line-height: 40rpx;
			}
		}
	}

	// 步骤内容
	.step-content {
		.u-form-item {
			margin-bottom: 40rpx;
		}
	}

	// 按钮样式
	.custom-style {
		margin-top: 40rpx;
		width: 100%;
		background-color: #409EFF;
		color: #fff;
		font-size: 32rpx;
		height: 80rpx;
		line-height: 80rpx;
		border: none;

		&.prev-button {
			background-color: #f0f0f0;
			color: #333;
		}

		&.submit-button {
			background-color: #67C23A;
		}
	}

	// 登录文字超链接样式
	.login-link {
		margin-top: 20rpx;
		text-align: center;
		color: #409EFF;
		font-size: 28rpx;
		text-decoration: underline;
		cursor: pointer;
	}
</style>